<?php
//call it when Octoshape pass back the result
require_once 'TVB/VideoInfrastructure/Server/requires.php';


function viOctoshapeCallbackStatus($export_id,$octo_text,$octo_status) {



$rf = VideoInfrastructure::getReadableRecordFactory();


$foundrecord = $rf->findInstance('Export', $export_id);
print_R($foundrecord);
$source_id = $foundrecord->source_id;
//$octo_status = 0;
//print_R($source_id);
print_R('source_id='.$source_id);
print_R('export_id='.$export_id);

	if ($foundrecord) {
        $rf = VideoInfrastructure::GetWritableRecordFactory();
		    // search old octoshape distribution record
		$octodistribution_entry_list = $rf->find('DistributionOcto', 'export_id=? AND source_id = ?',
			array($export_id, $source_id)
		);
print_R($octodistribution_entry_list);	
	    if (!$octodistribution_entry_list) {		
			// create new octoshape distribution record
			$distribution_octo_entry = new DistributionOcto();
			$distribution_octo_entry->export_id = $export_id;
			$distribution_octo_entry->source_id = $source_id;
			$distribution_octo_entry->octo_status = $octo_status;
			$distribution_octo_entry->create_time = date('Y-m-d H:i:s');			
			$distribution_octo_entry->id = $rf->insert($distribution_octo_entry);
			if ($distribution_octo_entry->id != 0) {
				error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
				error_log("ERROR - Create distribution octoshape record FAILS");
				error_log("Parameters: " . var_export($distribution_octo_entry, TRUE));
				
				return -3; // Create destination record FAILS
			}
		}else{
//		
print_R('octo status='.$octodistribution_entry_list[0]->octo_status);
			$current_octostatus=$octodistribution_entry_list[0]->octo_status;
			$octomod_entry = array_shift($octodistribution_entry_list);
			$octomod_entry->export_id = $export_id;
			$octomod_entry->source_id = $source_id;
			$octomod_entry->modify_time = null;

			// update meta		
			//0 is start 
			//1 is pending 
			//2 is live 
			//3 is redo
			if ($current_octostatus ==2){
			 $octo_status =3;
			 //echo 'status =3'.$current_octostatus;
			}else{
			 $octo_status =2;	
			 //echo 'status =2'.$current_octostatus;		 
			}
			$octomod_entry->octo_text = $octo_text;            		
			$octomod_entry->octo_status = $octo_status;
			$octomod_entry->pms_status = 1;


			// save
			$ret = $rf->save_by_fields($octomod_entry, array('export_id','source_id'));		
			if (!$ret) {
			error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
			error_log("ERROR - Update octoshape distribution record FAILS");

		   }	
//		
		}
	}else{
        error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
		error_log("call back ID from Octoshape {$export_id} is not correct ");
	}		
			

}
			
			// vi.api.tvb.com/octoshapecallbackstatus.php??file=example.mp7&&status=FAIL&text=cannot+determine+file+format
$export_id = isset($_REQUEST['export_id']) ? $_REQUEST['export_id'] : 138;
$octo_status = isset($_REQUEST['status']) ? $_REQUEST['status'] : 0;
$octo_text = isset($_REQUEST['text']) ? $_REQUEST['text'] : 0;
viOctoshapeCallbackStatus($export_id,$octo_text,$octo_status);


?>